package lesson24.server;

import io.grpc.ServerBuilder;
import lesson24.grpc.impl.StudentServiceImpl;

import java.io.IOException;
import java.util.Objects;

/**
 * 服务端<br/>
 * Created on : 2018-11-05 23:35
 * @author lizebin
 */
public class Server {

    private io.grpc.Server server;

    private void start() throws IOException {
        this.server = ServerBuilder.forPort(8888).addService(new StudentServiceImpl()).build().start();
        System.out.println("server start");
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                System.out.println("shutting down gRPC server");

            }
        });
    }

    private void stop() {
        if (Objects.nonNull(server)) {
            server.shutdown();
        }
    }

    private void blockUntialShutdown() throws InterruptedException {
        if (Objects.nonNull(server)) {
            server.awaitTermination();
        }
    }

}
